package domain;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.io.FileUtils;

/**
 * 域对象定义:关联的文件信息对象.
 * @author wuzh
 */
@Entity
@Table(name = "T_RELATED_FILE")
public class RelatedFile {
    /** 唯一标示 * */
    private Integer id;

    /**
     * 表示这个文件被关联的对象的唯一标示,表明这个文件属于这个关联对象.
     */
    private String relatedTo;

    /** 表示这个文件被关联的对象的类型 * */
    private String topic;

    /** 文件名（不带路径） * */
    private String fileName;

    /** 完整文件名（包含绝对路径) * */
    private String fullName;

    /** 文件大小（字节） * */
    private Integer size;

    /** 上传日期 * */
    private java.sql.Timestamp uploadedTime;

    /**
     * @return id
     */
    @Id
    @Column(name = "ID")
    @GeneratedValue
    public Integer getId() {
        return id;
    }

    /**
     * @return filename
     */
    @Column(name = "FILE_NAME", length = 128, nullable = false)
    public String getFileName() {
        return fileName;
    }

    /**
     * @return topic
     */
    @Column(name = "topic", length = 128, nullable = true)
    public String getTopic() {
        return topic;
    }

    /**
     * @return fullName
     */
    @Column(name = "FULL_NAME", length = 256, nullable = false)
    public String getFullName() {
        return fullName;
    }

    /**
     * @return size
     */
    @Column(name = "FILE_SIZE", nullable = false)
    public Integer getSize() {
        return size;
    }

    /**
     * 得到可以显示的字节数(3.4k)
     * @return string
     */
    @Transient
    public String getStrSize() {
        return FileUtils.byteCountToDisplaySize(size);
    }

    /**
     * @return uploadedTime
     */
    @Column(name = "UPLOADED_TIME", nullable = false)
    public java.sql.Timestamp getUploadedTime() {
        return uploadedTime;
    }

    /**
     * @return relatedTo
     */
    @Column(name = "RELATEDTO", length = 80, nullable = false)
    public String getRelatedTo() {
        return relatedTo;
    }

    /**
     * 判断是否图片.
     * @return boolean
     */
    @Transient
    public boolean isImage() {
        int ind = fileName.lastIndexOf('.');
        if (ind <= 0) {
            return false;
        }
        String ext = fileName.substring(ind+1);
        return ext.equalsIgnoreCase("gif") || ext.equalsIgnoreCase("jpeg")
                || ext.equalsIgnoreCase("jpg") || ext.equalsIgnoreCase("bmp")
                || ext.equalsIgnoreCase("png");
    }

    /**
     * set id
     * @param pId id
     */
    public void setId(Integer pId) {
        id = pId;
    }

    /**
     * set fileName
     * @param pFileName fileName
     */
    public void setFileName(String pFileName) {
        fileName = pFileName;
    }

    /**
     * set fullName
     * @param pFullName fullName
     */
    public void setFullName(String pFullName) {
        fullName = pFullName;
    }

    /**
     * set size
     * @param pSize size
     */
    public void setSize(int pSize) {
        size = pSize;
    }

    /**
     * set topic
     * @param pTopic pTopic
     */
    public void setTopic(String pTopic) {
        topic = pTopic;
    }

    /**
     * set uploadedTime
     * @param pUploadedTime uploadedTime
     */
    public void setUploadedTime(java.sql.Timestamp pUploadedTime) {
        uploadedTime = pUploadedTime;
    }

    /**
     * set relatedTo
     * @param pRelatedTo relatedTo
     */
    public void setRelatedTo(String pRelatedTo) {
        relatedTo = pRelatedTo;
    }

}
